<?php

// Dies entspricht error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
/*
 * A Class contains methods to connect or disconnect from Database.
 * @author Kamdem Kenmogne
 */
//require_once 'config.php';
require_once dirname(__FILE__) . './config.php';

class DatabaseConnect extends PDO {

    private $host = DB_HOST;
    private $user = DB_USER;
    private $pass = DB_PASSWORD;
    private $dbname = DB_DATABASE;
    private $dbh;
    private $dsn;
    private $error;
    private $options;

    // constructor
    function __construct() {
        //echo 'databaseconnect';
        // Set DSN
        $this->dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;

        // Set options
        $this->options = array(
            PDO::ATTR_PERSISTENT => true,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_EMULATE_PREPARES => TRUE
        );

        try {
            parent::__construct($this->dsn, $this->user, $this->pass, $this->options);
        } catch (PDOException $e) {
            print "Error!: " . $e->getMessage() . "<br/>";
            die();
        }
    }

    // destructor
    function __destruct() {
        $this->dbh = null;
    }

    public function connect() {
        try {
            $this->dbh = new PDO($this->dsn, $this->user, $this->pass, $this->options);
        } catch (PDOException $e) {
            print "Error!: " . $e->getMessage() . "<br/>";
        }
    }

    // Closing database connection
    public function close() {
        $this->dbh = null;
    }

}
